using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presenters;
using Dominio;
using System.Collections.Generic;

public partial class MessageBoard : AbstractView
{
    private MyAccountPresenter presenter;
    private IList<Question> questionList;
    public MessageBoard()
    {
        presenter = new MyAccountPresenter();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        this.LogedIn();
        this.getQuestions();
        questiondrop.Visible = ((User)Session["user"]).Role.isTester(((User)Session["user"]));
        
        
    }
    protected void newmessage_Click(object sender, EventArgs e)
    {
        if (((User)Session["user"]).Role.isTM(((User)Session["user"])))
        {
            questionlbl.Visible = true;
        }
        else
        {
            answerlbl.Visible = true;
        }
        Subject.Visible = true;
        Subjecttxt.Visible = true;
        questiontxt.Visible = true;
        savelink.Visible = true;
        cancellink.Visible = true;
    }
    protected void savelink_Click(object sender, EventArgs e)
    {
        if (((User)Session["user"]).Role.isTM(((User)Session["user"])))
        {
            presenter.createQuestion(Subjecttxt.Text, questiontxt.Text, Session["user"], (Bug)Session["selectedBug"]);
        }
        else
        {
            presenter.createAnswer( Subjecttxt.Text, 
                                    questiontxt.Text, 
                                    Session["user"], 
                                    presenter.getQuestionById(int.Parse(questiondrop.SelectedValue)));
        }
        Subject.Visible = false;
        Subjecttxt.Visible = false;
        questionlbl.Visible = false;
        questiontxt.Visible = false;
        savelink.Visible = false;
        cancellink.Visible = false;
        answerlbl.Visible = false;
        this.getQuestions();

    }
    protected void cancellink_Click(object sender, EventArgs e)
    {
        Subject.Visible = false;
        Subjecttxt.Visible = false;
        questionlbl.Visible = false;
        questiontxt.Visible = false;
        savelink.Visible = false;
        cancellink.Visible = false;
        answerlbl.Visible = false;
    }
    protected void message_Click(object sender, EventArgs e)
    {
        Response.Redirect("MessageBoard.aspx?BugId=" + ((Bug)Session["selectedBug"]).Id);
    }
    protected void details_Click(object sender, EventArgs e)
    {
        Response.Redirect("BugDetails.aspx?BugId=" + ((Bug)Session["selectedBug"]).Id);
    }

    protected void getQuestions()
    {
        questionList = presenter.getAllQuestion(Session["user"], ((Bug)Session["selectedBug"]).Id);

        foreach (Question question in questionList)
        {
            questiondrop.Items.Add(question.Id.ToString());

            Label Qid = new Label();
            Qid.Text = question.Id.ToString() + "-";
            Qid.CssClass = "tituloId";

            Label titulo = new Label();
            titulo.CssClass = "titulo";
            titulo.Text = question.Subject;

            Label date = new Label();
            date.Text = question.Creation.ToString();
            date.CssClass = "creation";

            Label pregunta = new Label();
            pregunta.CssClass = "pregunta";
            pregunta.Text = question.Description;

            Div.Controls.Add(Qid);
            Div.Controls.Add(date);
            Div.Controls.Add(titulo);
            Div.Controls.Add(pregunta);
            foreach (Answer answer in question.AnswerList)
            {
                Label fecha = new Label();
                fecha.Text = answer.Creation.ToString();
                fecha.CssClass = "creationAnswer";   
                Label tituoRespuesta = new Label();
                tituoRespuesta.CssClass = "tituloRespuesta";
                tituoRespuesta.Text = answer.Description;
                Label respuesta = new Label();
                respuesta.CssClass = "respuesta";
                respuesta.Text = answer.Description;
                Div.Controls.Add(fecha);
                Div.Controls.Add(tituoRespuesta);
                Div.Controls.Add(respuesta);
            }
        }
    }

    protected void responseLink_Click(object sender, EventArgs e)
    {
        Subject.Visible = true;
        Subjecttxt.Visible = true;
        questionlbl.Visible = true;
        questiontxt.Visible = true;
        savelink.Visible = true;
        cancellink.Visible = true;
    }
}
